<template>
  <!-- 表格展示 -->
  <div class="table-page-content">
    <z-table
      ref="table"
      rowKey="id"
      :showAlert="false"
      :showSelection="false"
      :loading="loading"
      :columns="columns0"
      :dataSource="pageData"
      @selectedRow="onSelectedRow"
      @change="onTableChange"
    >
    </z-table>
  </div>
</template>

<script>
import * as serviceApi from '@/api/sjsk/clinicmanageService'
import {downLoadFileReader} from '@/utils/export-util/util'

export default {
  components: {  },
  data() {
    return {
      toggle: true,
      loading: false,
      isDisabled: true,
      rescueTypeDictOptions: [
        { text: '失业保险金领取', value: 1, name: 'depositCollection' },
        { text: '灵活就业社保补贴', value: 2, name: 'socialSecuritySubsidy' },
        { text: '脱贫', value: 3, name: 'ridPoor' },
        { text: '低保救助', value: 4, name: 'subsistenceAssistance' },
        { text: '临时救助', value: 5, name: 'temporaryAssistance' },
        { text: '其他救助-慰问走访', value: 6, name: 'otherAssistance' },
        { text: '其他救助-子女教育', value: 7, name: 'otherAssistance' },
        { text: '其他救助-重大疾病帮助', value: 8, name: 'otherAssistance' },
      ],
      selectKeys: [],
      queryParam: {
        caseNo: this.$store.state.holographicArchives.routeObjData.sjskId,
        rescueType:[]
      },

      columns0:[],
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '救援时间',
          dataIndex: 'beginTime',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'detectNum' },
          // customRender: (text, record, index) => {
          //   // return record.beginTime && record.endTime ? record.beginTime +'~'+ record.endTime : '--'
          //   return record.beginTime && record.endTime ? record.beginTime : '--'
          // },
        },
        {
          title: '救助类型',
          dataIndex: 'rescueType',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <a-space size={6}>
                <a-button type="link" style={'padding:0'} onClick={(event) => this.handleRouter('details', record)}> 查看 </a-button>
              </a-space>
            )
          },
        },
      ],
      columns1: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '纳入时间',
          dataIndex: 'beginTime',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'detectNum' },
          // customRender: (text, record, index) => {
          //   // return record.beginTime && record.endTime ? record.beginTime +'~'+ record.endTime : '--'
          //   return record.beginTime && record.endTime ? record.beginTime : '--'
          // },
        },
        {
          title: '状态',
          dataIndex: 'rescueStatus',
          width: 80,
          align: 'center',
          customRender: text => {
            // return this.$filterDictTextByCache('rescue_type', text)
            if(text === 1){
              return '未开始'
            }
            if(text === 2){
              return '进行中'
            }
            if(text === 3){
              return '已完成'
            }
            if(text === 4){
              return '停止'
            }
          }
        },
        {
          title: '救助类型',
          dataIndex: 'rescueType',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '家庭成员（人数）',
          dataIndex: 'rescueType',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '户籍地祥址',
          dataIndex: 'birthAddress',
          width: 80,
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={14} />
          },
        },
        {
          title: '现在居住地祥址',
          dataIndex: 'permanentAddress',
          width: 80,
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={14} />
          },
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <a-space size={6}>
                <a-button type="link" style={'padding:0'} onClick={(event) => this.handleRouter('details', record)}> 查看 </a-button>
              </a-space>
            )
          },
        },
      ],
      columns2: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '状态',
          dataIndex: 'rescueStatus',
          width: 80,
          align: 'center',
          customRender: text => {
            // return this.$filterDictTextByCache('rescue_type', text)
            if(text === 1){
              return '未开始'
            }
            if(text === 2){
              return '进行中'
            }
            if(text === 3){
              return '已完成'
            }
            if(text === 4){
              return '停止'
            }
          }
        },
        {
          title: '纳入时间',
          dataIndex: 'beginTime',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'detectNum' },
          // customRender: (text, record, index) => {
          //   // return record.beginTime && record.endTime ? record.beginTime +'~'+ record.endTime : '--'
          //   return record.beginTime && record.endTime ? record.beginTime : '--'
          // },
        },
        {
          title: '家庭成员（人数）',
          dataIndex: 'rescueType1',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '户籍发放低保金（元）',
          dataIndex: 'birthAddress',
          width: 80,
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={14} />
          },
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <a-space size={6}>
                <a-button type="link" style={'padding:0'} onClick={(event) => this.handleRouter('details', record)}> 查看 </a-button>
              </a-space>
            )
          },
        },
      ],
      columns3:   [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '状态',
          dataIndex: 'rescueStatus',
          width: 80,
          align: 'center',
          customRender: text => {
            // return this.$filterDictTextByCache('rescue_type', text)
            if(text === 1){
              return '未开始'
            }
            if(text === 2){
              return '进行中'
            }
            if(text === 3){
              return '已完成'
            }
            if(text === 4){
              return '停止'
            }
          }
        },
        {
          title: '救助时间',
          dataIndex: 'beginTime',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'detectNum' },
          // customRender: (text, record, index) => {
          //   // return record.beginTime && record.endTime ? record.beginTime +'~'+ record.endTime : '--'
          //   return record.beginTime && record.endTime ? record.beginTime : '--'
          // },
        },
        {
          title: '家庭成员（人数）',
          dataIndex: 'rescueType',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '合计发放救助金（元）',
          dataIndex: 'birthAddress',
          width: 80,
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={14} />
          },
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <a-space size={6}>
                <a-button type="link" style={'padding:0'} onClick={(event) => this.handleRouter('details', record)}> 查看 </a-button>
              </a-space>
            )
          },
        },
      ],
      columns4: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '帮扶时间',
          dataIndex: 'beginTime',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'detectNum' },
          // customRender: (text, record, index) => {
          //   // return record.beginTime && record.endTime ? record.beginTime +'~'+ record.endTime : '--'
          //   return record.beginTime && record.endTime ? record.beginTime : '--'
          // },
        },
        {
          title: '救助类型',
          dataIndex: 'rescueType',
          width: 80,
          align: 'center',
          customRender: text => {
            return this.$filterDictTextByCache('rescue_type', text)
          }
        },
        {
          title: '操作',
          width: 60,
          align: 'center',
          dataIndex: 'action',
          customRender: (text, record, index) => {
            return (
              <a-space size={6}>
                <a-button type="link" style={'padding:0'} onClick={(event) => this.handleRouter('details', record)}> 查看 </a-button>
              </a-space>
            )
          },
        },
      ],



      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {}
    }
  },
  created() {},
  async mounted() {

  },
  // watch: {
  //   // 组织机构树-联动筛选人员列表
  //   'queryParam.departId'(newVal, oldVal){
  //     this.onQuery()
  //   }
  // },
  methods: {
    succourListFT(val){
      // let a = 1
      if(val === 0){
        this.columns0 = this.columns
      }
      if(val === 1){
        this.columns0 = this.columns1
      }
      if(val === 2){
        this.columns0 = this.columns2
      }
      if(val === 3){
        this.columns0 = this.columns3
      }
      if(val === 4){
        this.columns0 = this.columns4
      }
    },
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.queryParam = {}
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.refreshTable()
    },
    onSelectedRow(keys) {
      this.selectKeys = keys
      this.isDisabled = keys.length > 0 ? false : true
    },
    onTableChange(pagination, pageFilter, sorter) {
      this.pageFilter = Object.assign({}, this.pageFilter, pagination)
      this.refreshTable()
    },
    refreshTable(val) {

      let {time, ...dataParam} = this.queryParam // 剔除time元素，dataParam最新的对象
      let queryParam = Object.assign({}, this.queryParam, this.pageFilter)
      if(val !== null){
        console.log(val)
        // let queryParam = Object.assign({}, {rescueType:val})
        this.queryParam.rescueType = val
      }
      this.loading = true
      serviceApi
        .getRescueList(queryParam)
        .then((res) => {
          this.loading = false
          console.log(res)
          if (res.success) {
            this.pageData = res.result ? res.result : {}
            this.$refs.table.onClearRowKeys()
          } else {
            this.loading = false
          }
        })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    // 导出
    handleExport(record) {
      let that = this
      let selectList = this.$refs.table.getSelectedRows()
      let idList1 = selectList.map(v => v.id).filter(obj => obj !== null && obj !== undefined && obj !== '')
      let idList2 = record && Object.keys(record).length > 0 && record.id ? [record.id] : []
      let params = Object.assign(
        {},
        {
          idList: selectList.length > 0 ? idList1 : idList2,
          // querySjskPersonVO: this.queryParam
        }
      )
      this.$confirm({
        title: `是否确认导出？`,
        okText: '确定',
        onOk() {
          serviceApi.exportRescue(params).then(async res => {
            const fileName = '帮扶救助记录列表数据.xlsx'
            await downLoadFileReader(res, fileName)
            that.$refs.table.onClearRowKeys()
          })
        }
      })
    },
    handleRouter(type, record) {
      let path = null
      if(record && record instanceof Object) {
        let col = this.rescueTypeDictOptions.find((col, i) => col.value === record.rescueType)
        let name = record instanceof Object?col.name: col
        let id = record instanceof Object?record.id: record
        path = type=='edit'?`/help-rescue/${name}/edit/${id}`:`/help-rescue/${name}/details/${id}`
      } else {
        path = type=='add'?`/help-rescue/${record}/add`:``
      }
      if(!path) return
      this.$router.push({
        path: path,
        query: {}
      })
    },
    // 获取组件的时间， prop为对应的字段，date为选中的时间值
    getChangeTime(prop, date) {
      if(prop === 'time') {
        this.$set( this.queryParam, 'beginTime', date[0] )
        this.$set( this.queryParam, 'endTime', date[1] )
      }
    },
    getTreeData(datas, id) {
      console.log(datas, id)
      console.log(id)
      this.$set( this.queryParam, 'departId', id )
    },
    toggleBool(bool) {
      console.log(bool)
      this.toggle = bool
    },
  }
}
</script>
<style lang="less" scoped>
</style>
